# program equivalent to aclient.py using XMLRPC
import time
from twisted.internet import reactor
from twisted.web import xmlrpc

def print_result(result, a, b):
    print '%d * %d = %d' % (a, b, result)

def print_error(failure):
    print 'ERROR: %s' % failure

def print_time_exit(_, t0, t1):
    print 'elapsed = %.3f' % (t1-t0)
    reactor.stop()

def run(calc):
    t0 = time.time()
    for i in xrange(11):
        for j in xrange(11):
            d = calc.callRemote('prod', i, j)
            d.addCallbacks(print_result, print_error, callbackArgs=[i, j])
            print 'fired %d' % id(d)
    t1 = time.time()
    print 'adding final callback to %d' % id(d)
    d.addCallbacks(print_time_exit, print_error, callbackArgs=[t0, t1])

def start():
    print 'starting server'
    s = xmlrpc.Proxy('http://localhost:5673/')
    reactor.callLater(1, run, s)

reactor.callWhenRunning(start)
reactor.run()
